package com.lin.xd.sign;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText editText = (EditText)findViewById(R.id.editText);
        EditText editText1 = (EditText)findViewById(R.id.editText1);

        editText1.setMinEms(6);
        editText1.setMaxEms(30);

        final Pattern number = Pattern.compile("\\d+");
        final Pattern pPhone =Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        final Pattern pMail = Pattern.compile("^[\\w&&[^_]]+@[\\w&&[^_]]+\\.com$");
        final Pattern pPass = Pattern.compile("[\\w&&[^_]]+");

        editText1.setFilters(new InputFilter[] {
                new InputFilter.LengthFilter(30)
        });

        editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    EditText text = (EditText)v;
                    String s = text.getText().toString();
                    if(s.equals("")) {
                        Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    } else {
                        if(s.length()<6) {
                            Toast.makeText(MainActivity.this,"密码至少为六位",Toast.LENGTH_SHORT).show();
                        } else {
                            Matcher matcher = pPass.matcher(s);
                            if(!matcher.matches()) {
                                Toast.makeText(MainActivity.this,"密码只能为数字或字母",Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }
            }
        });

        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus) {
                    EditText text1 = (EditText)v;
                    String s = text1.getText().toString();
                    if (s.equals("")) {
                        Toast.makeText(MainActivity.this,"请输入账号",Toast.LENGTH_SHORT).show();
                    } else {
                        Matcher mNumber = number.matcher(s);
                        if (mNumber.matches()) {
                            Matcher mPhone = pPhone.matcher(s);
                            if(!mPhone.matches()) {
                                text1.setTextColor(getResources().getColor(R.color.red));
                                Toast.makeText(MainActivity.this,"手机号输入有误",Toast.LENGTH_SHORT).show();
                            } else {
                                text1.setTextColor(getResources().getColor(R.color.black));
                            }
                        } else {
                            Matcher mMail = pMail.matcher(s);
                            if(!mMail.matches()) {
                                text1.setTextColor(getResources().getColor(R.color.red));
                                Toast.makeText(MainActivity.this,"邮箱输入有误",Toast.LENGTH_SHORT).show();
                            } else {
                                text1.setTextColor(getResources().getColor(R.color.black));
                            }
                        }
                    }
                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
